package praca.impl;

import java.util.ArrayList;
import java.util.Iterator;

import android.util.Log;
import praca.framework.Sprite;
import praca.framework.SpriteBatcher;
import praca.framework.TextureAtlas;
import praca.framework.TextureGL;
import praca.framework.TouchEventGL;
import praca.framework.Vector;
import praca.framework.Vertices;
import praca.interfejsy.Game;
import praca.interfejsy.Screen;
import praca.interfejsy.Texture;
import praca.interfejsy.TouchEvent;

//-------------CALA PLANSZA KLOCKOW-------------------//
public class GameScreen3 extends Screen{

	
	Vertices brickVert = new Vertices(60, 48, true); // w poziomie 60*12=720 i zostaje 80 pikseli na pingwina,  klockow w pionie 10
	Vector gravity = new Vector(0,-400);
	Vector brickPos = new Vector(240,450);
	Texture brickTexture;
	TouchEvent event;
	boolean isTouched = false;
	final int NR_BRICKS = 8;
	final int NR_WALLS = 11;
	
	ArrayList<ArrayList<Brick>> walls; //wall0,wall1,wall2,wall3,wall4,wall5,wall6,wall7,wall8,wall9,wall10,wall11;
	
	//batcher_sprite_pragnienie
	TextureAtlas klockiAtlas;
	SpriteBatcher spriteBatcher;
	Sprite blue,green,red,yellow;
	
	
	public GameScreen3(Game game) {
		super(game);
		blue = new Sprite(klockiAtlas,0,0,64,64);
		green = new Sprite(klockiAtlas,64,64,128,128);
		red = new Sprite(klockiAtlas,128,128,192,192);
		yellow = new Sprite(klockiAtlas,192,192,256,256);
		walls= new ArrayList<ArrayList<Brick>>();
		
		for(int j=0 ; j<=NR_WALLS ; j++)
			walls.add(new ArrayList<Brick>());
		int whichwall = 0;
		for(ArrayList<Brick> bricks : walls){
			for(int i = 0; i < NR_BRICKS; i++) {
				int randomColor = 1 + (int)(Math.random() * ((4 - 1) + 1));
				bricks.add(new Brick(60,48,whichwall,i,randomColor)); 
			}
			whichwall+=1;
		}
		
	}
	
	@Override
	public void update(float deltaTime) {
		
		
		
		if(!in.getTouchEvents().isEmpty()){
			event = in.getTouchEvents().remove(in.getTouchEvents().size()-1);
				for(int j = 0; j < in.getTouchEvents().size(); j++){
					in.getTouchEvents().remove(in.getTouchEvents().size()-1);
				}

			
			if(event.getType() == TouchEventGL.TOUCH_DOWN)
			{
				//Log.d("touch", "Touch down");
				for(ArrayList<Brick> bricks : walls)
				{
				Iterator<Brick> it = bricks.iterator();
				Iterator<Brick> it2 = bricks.iterator();
					while(it.hasNext())
					{
						Brick brick = it.next();
						System.out.println("pierwszy while posY: "+brick.posY);
						if(brick.isInside(event.getX(), event.getY()))
						{
							//int pos = brick.posY; // nie wiem czy nie powinienem tego robic getem
							it2= it;
							it.remove();
							while(it2.hasNext())
							{
								Brick higherBrick = it2.next();
								System.out.println("drugi while posY: "+higherBrick.posY+" y: "+higherBrick.y);
								higherBrick.y= higherBrick.y - higherBrick.height;
								higherBrick.posY=higherBrick.posY-1;
								System.out.println("drugi while po zmianie posY: "+higherBrick.posY+" y: "+higherBrick.y);
							}
							
							//brick.isTouched=true;
							break;
						}
					}
				}
				
			}
			
			in.free(event);	
	}//koniec if
		
		g.drawColor(0, 1, 0.4f); //rysujemy zielone tlo
		
		
		//spriteBatcher.startBatch();
		for(ArrayList<Brick> bricks : walls)
		{
			for(Brick brick : bricks)
			{
					switch(brick.color)
					{
					    case 1:
					    	//System.out.println("x: " + brick.x+"y: "+brick.y);
					    	spriteBatcher.startBatch();
					    	spriteBatcher.drawSprite(blue, 60, 48, brick.x, brick.y);
					    	spriteBatcher.endBatch();
					    	break;
					 
					    case 2:
					    	//System.out.println("x: " + brick.x+"y: "+brick.y);
					    	spriteBatcher.startBatch();
					    	spriteBatcher.drawSprite(green, 60, 48, brick.x, brick.y);
					    	spriteBatcher.endBatch();
					    	break;
					    
					    case 3:
					    	//System.out.println("x: " + brick.x+"y: "+brick.y);
					    	spriteBatcher.startBatch();
					    	spriteBatcher.drawSprite(red, 60, 48, brick.x, brick.y);
					    	spriteBatcher.endBatch();
					    	break;
						      
					    case 4:
					    	//System.out.println("x: " + brick.x+"y: "+brick.y);
					    	spriteBatcher.startBatch();
					    	spriteBatcher.drawSprite(yellow, 60, 48, brick.x, brick.y);
					    	spriteBatcher.endBatch();
					    	break;
					    default:
					      System.out.println("nieprzewidziana sytuacja");
					}
					
	//				g.clearTransformations();
	//				g.moveTexture(brick.x, brick.y);
	//				g.drawTexture(brickTexture, brickVert);
			}
			
		}
		//spriteBatcher.endBatch();
//		if(!isTouched)
//		g.drawTexture(brick, brickVert);
		
		
	}

	@Override
	public void pause() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void resume() {
		/*
		 * Tekstury nalezy wczytywac w metodzie resume poniewaz po kazdym zablokowaniu telefonu
		 * itp zostaja one wyrzucone z pamieci
		 * korzystamy z obiektu TextureGL
		 */
		//brickTexture = new TextureGL(game, "brick1.png" );
		//troll = new TextureGL(game, "troll.png" );//nazwa pliku graficznego w katalogu ASSETS
		
		klockiAtlas = new TextureAtlas("klocki.png", game);
		spriteBatcher = new SpriteBatcher(klockiAtlas, game, 20);
		g.setTextureTransparentBackground(); //tlo KAZDEJ tekstury bedzie prezroczyste
		
	}

	@Override
	public void dispose() {
		// TODO Auto-generated method stub
		
	}
	
	public boolean czyDotknieto(float x, float y, int width, int height, float textureX, float textureY ){
		/*
		 * Prosta matematyka. Punkt polozenia to srodek obrazka
		 * ma on 100 szerokosci i 100 wysokosci
		 */
		Log.d("velocity", "Test: x="+x+" ,y="+y);//Uzycie LogCat!!
		
		if(x < textureX+width && x > textureX-width && y > textureY-height && y < textureY+height)
			return true;
		
		return false;
	}

}
